#include "std_err.h"

static const err_map_t _errt[] = {
	{STD_ERR_OK                , "STD_ERR_OK"                , "OK (no error)"},
	{STD_ERR_FAIL              , "STD_ERR_FAIL"              , "Generic failure"},
															 
	{STD_ERR_NO_MEM            , "STD_ERR_NO_MEM"            , "Out of memory"},
	{STD_ERR_INVALID_ARG       , "STD_ERR_INVALID_ARG"       , "Invalid argument"},
	{STD_ERR_INVALID_STATE     , "STD_ERR_INVALID_STATE"     , "Invalid state"},
	{STD_ERR_INVALID_SIZE      , "STD_ERR_INVALID_SIZE"      , "Invalid size"},
	{STD_ERR_NOT_FOUND         , "STD_ERR_NOT_FOUND"         , "Requested resource not found"},
	{STD_ERR_NOT_SUPPORTED     , "STD_ERR_NOT_SUPPORTED"     , "Operation or feature not supported"},
	{STD_ERR_TIMEOUT           , "STD_ERR_TIMEOUT"           , "Operation timed out"},
	{STD_ERR_INVALID_RESPONSE  , "STD_ERR_INVALID_RESPONSE"  , "Received response was invalid"},
	{STD_ERR_INVALID_CRC       , "STD_ERR_INVALID_CRC"       , "CRC or checksum was invalid"},
	{STD_ERR_INVALID_VERSION   , "STD_ERR_INVALID_VERSION"   , "Version was invalid"},
	{STD_ERR_INVALID_MAC       , "STD_ERR_INVALID_MAC"       , "MAC address was invalid"},
															 
	{STD_ERR_INVALID_IF_TYPE   , "STD_ERR_INVALID_IF_TYPE"   , "Interface type was invalid"},
	{STD_ERR_INVALID_SRC_ADDR  , "STD_ERR_INVALID_SRC_ADDR"  , "Source address was invalid"},
	{STD_ERR_INVALID_DST_ADDR  , "STD_ERR_INVALID_DST_ADDR"  , "Destination address was invalid"},
	{STD_ERR_INVALID_FRAME_LEN , "STD_ERR_INVALID_FRAME_LEN" , "Invalid frame length"},
	{STD_ERR_INVALID_PARAM_LEN , "STD_ERR_INVALID_PARAM_LEN" , "Invalid parameter length"},
	{STD_ERR_BUFFER_OVERFLOW   , "STD_ERR_BUFFER_OVERFLOW"   , "Buffer overflow"},
	{STD_ERR_BUFFER_UNDERFLOW  , "STD_ERR_BUFFER_UNDERFLOW"  , "Buffer underflow"},
	{STD_ERR_INVALID_COMMAND   , "STD_ERR_INVALID_COMMAND"   , "Invalid command"},
															 
	{STD_ERR_EOF               , "STD_ERR_EOF"               , "End of file"},
	{STD_ERR_UNCLOSED_STRING   , "STD_ERR_UNCLOSED_STRING"   , "Unclosed string"},
	{STD_ERR_ILLEGAL_CHAR      , "STD_ERR_ILLEGAL_CHAR"      , "Illegal character"},
	{STD_ERR_TOO_MUCH_CMDPARTS , "STD_ERR_TOO_MUCH_CMDPARTS" , "Too much command parts"},
	{STD_ERR_TOO_MUCH_CMDARGS  , "STD_ERR_TOO_MUCH_CMDARGS"  , "Too much command args"},
	{STD_ERR_TOO_LARGE_STR     , "STD_ERR_TOO_LARGE_STR"     , "String is oversize"},
	{STD_ERR_OUT_OF_RANGE      , "STD_ERR_OUT_OF_RANGE"      , "Value is out of range"},
	{STD_ERR_INTERVAL_TIMEOUT  , "STD_ERR_INTERVAL_TIMEOUT"  , "Timeout in operation interval"},
	{STD_ERR_BUFFER_OVERRUN    , "STD_ERR_BUFFER_OVERRUN"    , "Buffer overrun"},
	{STD_ERR_EXPIRED           , "STD_ERR_EXPIRED"           , "Resource is expired"},
	{STD_ERR_PARTIALLY_EXPIRED , "STD_ERR_PARTIALLY_EXPIRED" , "Resource is partially expired"},
	
	{STD_ERR_SYNTAX            , "STD_ERR_SYNTAX"            , "Syntax error"},
	{STD_ERR_NOT_ACCESSIBLE    , "STD_ERR_NOT_ACCESSIBLE"    , "Resource is not accessible"},
	{STD_ERR_INVALID_ID        , "STD_ERR_INVALID_ID"        , "Invalid identifier"},
	{STD_ERR_DISCONNECTED      , "STD_ERR_DISCONNECTED"      , "Connection is not established"},
	{STD_ERR_NO_RESOURCES      , "STD_ERR_NO_RESOURCES"      , "Out of resources"},
	{STD_ERR_LOCKED            , "STD_ERR_LOCKED"            , "Resource is locked"},
	{STD_ERR_NO_LOCK           , "STD_ERR_NO_LOCK"           , "No lock held"},
	{STD_ERR_IO                , "STD_ERR_IO"                , "IO error"},
	{STD_ERR_INVALID_ADDR      , "STD_ERR_INVALID_ADDR"      , "Invalid address"},
	{STD_ERR_ABORT             , "STD_ERR_ABORT"             , "Abort"},
	{STD_ERR_OCCUPIED          , "STD_ERR_OCCUPIED"          , "Resource is occupied"},
};

const char* std_err_name(std_err_t err) {
	int count = sizeof(_errt)/sizeof(_errt[0]);
	for (int i = 0; i < count; i++) {
		if (err == _errt[i].err) return _errt[i].name;
	}
	return "(Unknown)";
}

const char* std_err_comment(std_err_t err) {
	int count = sizeof(_errt)/sizeof(_errt[0]);
	for (int i = 0; i < count; i++) {
		if (err == _errt[i].err) return _errt[i].comment;
	}
	return "(Unregistered error)";
}
